﻿//Project: CassiniWrapper
//Martin CLAVREUIL
//martin@clavreuil.com

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CassiniWrapper
{
    public partial class MainForm : Form
    {
        NetworkPortManager netPortMan = new NetworkPortManager();

        public MainForm()
        {
            InitializeComponent();
        }

        private string _TargetFolderPath;
        public string TargetFolderPath
        {
            get { return _TargetFolderPath; }
            set { _TargetFolderPath = value; }
        }

        void CatchedNextPort(int port)
        {
            try
            {
                TxtServerPort.Text = netPortMan.GetNextAvailablePort(port).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Languages.Languages.GetString("FORM_Caption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = Languages.Languages.GetString("FORM_Caption");
            LblServerPort.Text = Languages.Languages.GetString("FORM_ServerPort");
            LblVirtualDirectory.Text = Languages.Languages.GetString("FORM_VDirectory");
            BtnLaunch.Text = Languages.Languages.GetString("FORM_Launch");
            BtnCancel.Text = Languages.Languages.GetString("FORM_Cancel");
            CatchedNextPort(CassiniWrapper.Properties.Settings.Default.FirstPortToCheck);
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void BtnLaunch_Click(object sender, EventArgs e)
        {
            int port;
            if (!TxtVirtualDirectory.Text.StartsWith("/")) TxtVirtualDirectory.Text = "/" + TxtVirtualDirectory.Text;
            if (Int32.TryParse(TxtServerPort.Text, out port))
            {
                try
                {
                    if (netPortMan.IsAvailable(port))
                    {
                        if (Uri.IsWellFormedUriString("http://localhost:" + port + TxtVirtualDirectory.Text, UriKind.Absolute))
                        {
                            System.Diagnostics.Process shell = new System.Diagnostics.Process();
                            shell.StartInfo.FileName = CassiniWrapper.Properties.Settings.Default.CassiniEXEPath;
                            shell.StartInfo.Arguments = "/port:" + port + " /path:\"" + this.TargetFolderPath + "\" /vpath:" + TxtVirtualDirectory.Text;
                            shell.Start();
                            Application.Exit();
                        }
                        else
                        {
                            MessageBox.Show(Languages.Languages.GetString("ERR_VDirTypeMismatch"), Languages.Languages.GetString("FORM_Caption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show(Languages.Languages.GetString("ERR_AlreadyUserPort"), Languages.Languages.GetString("FORM_Caption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                        CatchedNextPort(port);
                    }
                }catch{
                    MessageBox.Show(Languages.Languages.GetString("ERR_InvalidPort"), Languages.Languages.GetString("FORM_Caption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show(Languages.Languages.GetString("ERR_NotANumericPort"), Languages.Languages.GetString("FORM_Caption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
	
    }
}
